package com.example.userserver.service;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.example.userserver.entity.UserInfo;
import com.example.userserver.enums.ReturnEnum;
import com.example.userserver.manager.UserInfoManager;
import com.example.userserver.params.BalanceParams;
import com.example.userserver.params.PageUserInfoParams;
import com.example.userserver.params.UserInfoParams;
import com.example.userserver.utils.BeanCopyUtils;
import com.example.userserver.utils.Md5Utils;
import com.example.userserver.vo.LoginVO;
import com.example.userserver.vo.Result;
import com.example.userserver.vo.UserInfoVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class UserInfoService {
    @Autowired
    private UserInfoManager userInfoManager;

    public PageInfo<UserInfoVO> pageUserInfo(PageUserInfoParams pageUserInfoParams){
        if(StringUtils.isNotEmpty(pageUserInfoParams.getOrderBy())){
            pageUserInfoParams.setOrderBy("u."+pageUserInfoParams.getOrderBy());
        }
        PageHelper.startPage(pageUserInfoParams);
        List<UserInfoVO> list = userInfoManager.listUserInfo(pageUserInfoParams);
        PageInfo<UserInfoVO> pageInfo = new PageInfo(list);
        return pageInfo;
    }

    public UserInfoVO getUserInfo(String id) {
        return userInfoManager.getUserInfo(id);
    }

    public void deleteUserInfo(String id) {
        userInfoManager.deleteUserInfo(id);
    }

    public Result updateUserInfo(UserInfoParams userInfoParams) {
        UserInfoVO userInfoVO = userInfoManager.getUserInfo(userInfoParams.getId());
        // 没有改变不需要判重
        // 用户名不能重复
        if( ! userInfoVO.getUserName().equals(userInfoParams.getUserName())){
            if(userInfoManager.checkUserName(userInfoParams.getUserName())){
                return new Result(null, ReturnEnum.USERNAME_RE);
            }
        }
        // 同个学校中的学号唯一
        if((StringUtils.isEmpty(userInfoVO.getSchoolId()) && StringUtils.isEmpty(userInfoVO.getStudentId())) ||
                ! userInfoVO.getSchoolId().equals(userInfoParams.getSchoolId()) || ! userInfoVO.getStudentId().equals(userInfoParams.getStudentId())){
            if(userInfoManager.checkStudentId(userInfoParams.getSchoolId(), userInfoParams.getStudentId())){
                return new Result(null, ReturnEnum.STUDENTID_RE);
            }
        }

        UserInfo userInfo = new UserInfo();
        if(userInfoParams.getPhoto() == "1"){
            userInfoParams.setPhoto(null);
        }
        if(userInfoParams.getRole() == 0){
            userInfoParams.setRole(userInfoVO.getRole());
        }
        if(userInfoParams.getStatus() == 0){
            userInfoParams.setStatus(userInfoVO.getStatus());
        }
        if(userInfoParams.getCityId() == ""){
            userInfoParams.setCityId(null);
        }
        if(userInfoParams.getSchoolId() == ""){
            userInfoParams.setSchoolId(null);
        }
        BeanCopyUtils.copyPropertiesIgnoreNull(userInfoParams, userInfo);
        userInfoManager.updateUserInfo(userInfo);
        LoginVO loginVO = new LoginVO(userInfo.getId(), userInfo.getUserName(), userInfoParams.getToken(), userInfo.getPhoto(), userInfo.getRole()); // 更新session
        return new Result(loginVO, ReturnEnum.SUCCESS);
    }

    public Result insertUserInfo(UserInfoParams userInfoParams) {
        UserInfo userInfo = new UserInfo();
        BeanCopyUtils.copyPropertiesIgnoreNull(userInfoParams, userInfo);
        userInfoManager.insertUserInfo(userInfo);
        return new Result(null, ReturnEnum.PASSWORD_WRONG);
    }

    public Result updatePassword(UserInfoParams userInfoParams){
        UserInfo userInfo = userInfoManager.getUserInfoDO(userInfoParams.getId());
        String hashedOldPassword = getHashedPassword(userInfoParams.getOldPassword(), userInfo.getSalt());
        if( ! hashedOldPassword.equals(userInfo.getPassword())){
            return new Result(null, ReturnEnum.PASSWORD_WRONG);
        }
        // 加密
        String hashedPassword = getHashedPassword(userInfoParams.getPassword(), userInfo.getSalt());
        userInfo.setPassword(hashedPassword);
        userInfoManager.updateUserInfo(userInfo);
        return new Result(null , ReturnEnum.SUCCESS);
    }

    public Result updateBalance(BalanceParams balanceParams) {
        UserInfo userInfo = userInfoManager.getUserInfoDO(balanceParams.getId());
        Double fin; // 最终金额
        if(balanceParams.getStatus() == 1){ // 充值
            fin = userInfo.getBalance() + balanceParams.getAmount();
        } else{
            if(userInfo.getBalance() < balanceParams.getAmount()){
                return new Result(null, ReturnEnum.BALACNE_NOT_ENOUGH);
            }
            fin = userInfo.getBalance() - balanceParams.getAmount();
        }
        userInfo.setBalance(fin);
        userInfoManager.updateUserInfo(userInfo);
        return new Result(null , ReturnEnum.SUCCESS);
    }

    private String getHashedPassword(String password, String salt) {
        return Md5Utils.generateMd5(String.format("%s%s", password, salt));
    }

    public Result getIsComplete(String id) {
        UserInfo userInfo = userInfoManager.getUserInfoDO(id);
        if(StringUtils.isEmpty(userInfo.getSchoolId())){
            return new Result(false, ReturnEnum.SUCCESS);
        }
        return new Result(true, ReturnEnum.SUCCESS);
    }


}
